package com.jye.rseven.user.controller;


import com.jye.rseven.domain.ChargeRecord;
import com.jye.rseven.domain.User;
import com.jye.rseven.service.ChargeRecordService;
import com.jye.rseven.domain.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;

@RestController
@RequestMapping("/chargeRecord")
@Api(tags = "佣金控制器API")

public class ChargeRecordController {

    @Autowired
    ChargeRecordService chargeRecordService;


    /*
     * 通过用户id查询我的推广人数
     *
     * */
    @GetMapping("/findChargeRecordByuId")
    @ApiOperation(value="查询我的推广人数", notes="根据用户id查询我的推广人数", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "uId",
                    value = "用户ID", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findChargeRecordByuId(int uId){
        JsonResult jsonResult = new JsonResult();
        Integer c= chargeRecordService.findChargeRecordByuId(uId);
        if (!StringUtils.isEmpty(c)){
            jsonResult.setData(c);
        }else{
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }

    /*
     * 通过uId查找用户的所有佣金记录
     * */
    @GetMapping("/findRecordByuId")
    @ApiOperation(value = "查找用户的所有佣金记录", notes = "通过uId查找佣金记录", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "uId",
                    value = "用户ID", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findRecordByuId(int uId) {
        JsonResult jsonResult = new JsonResult();
        List<ChargeRecord> list = chargeRecordService.findRecordByuId(uId);
        if (list != null) {
            jsonResult.setData(list);
        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }

    /*
     * 查找我的推广的用户信息
     * */
    @GetMapping("/findMyChargeRecord")
    @ApiOperation(value = "查找我的推广的用户信息", notes = "根据uId查询出记录表的pId，再根据pId去查询被推广表的用户id", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "uId",
                            value = "用户ID", required = true,
                            dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "skip",
                    value = "跳过多少条", required = true,
                    dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "limit",
                    value = "限制多少条", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findMyChargeRecord(int uId) {
        JsonResult jsonResult = new JsonResult();
        List<User> list = chargeRecordService.findMyChargeRecord(uId);
        if (list != null) {
            jsonResult.setData(list);
        } else {
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }


    /*
     * 生成用户佣金记录
     * */
    @PostMapping("/addChargeRecord")
    @ApiOperation(value = "生成用户佣金记录", notes = "通过pId和uId生成用户佣金记录", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "chargeRecord",
                    value = "佣金记录实体类", required = true,
                    dataType = "com.jye.rseven.user.domain.ChargeRecord"),
    })
    public JsonResult addChargeRecord(ChargeRecord chargeRecord) {
        JsonResult jsonResult = new JsonResult();
        String msg=chargeRecordService.addChargeRecord(chargeRecord);
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    /*
     * 修改佣金记录
     * */
    @PutMapping("/updateChargeRecord")
    @ApiOperation(value = "修改佣金记录", notes = "修改佣金记录,只能修改佣金、状态和时间", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "chargeRecord",
                    value = "佣金记录实体类", required = true,
                    dataType = "com.jye.rseven.user.domain.ChargeRecord"),
    })
    public JsonResult updateChargeRecord(ChargeRecord chargeRecord) {
        JsonResult jsonResult = new JsonResult();
        System.out.println(chargeRecord);
        int i = chargeRecordService.updateChargeRecord(chargeRecord);
        if (i == 1) {
            jsonResult.setMsg("修改成功!");
        } else {
            jsonResult.setMsg("修改失败!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }


    /*
     * 通过用户id查询用户的有效总佣金
     *
     * */
    @GetMapping("/findchargesByuId")
    @ApiOperation(value="查询用户的有效总佣金", notes="根据用户id查询用户的有效总佣金", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "uId",
                    value = "用户ID", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findChargesByuId(int uId){
        JsonResult jsonResult = new JsonResult();
        Integer c= chargeRecordService.findChargesByuId(uId);
        if (!StringUtils.isEmpty(c)){
            jsonResult.setData(c);
        }else{
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }


}
